﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_HisFin_HisFin002a : AdminBasePage
{
    decimal? receiptId;
    decimal? boxId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["BoxId"] != null)
        {
            boxId = decimal.Parse(Request.QueryString["BoxId"]);
        }
        if (Request.QueryString["ReceiptID"] != null)
        {
            receiptId = decimal.Parse(Request.QueryString["ReceiptID"]);
        }

        if (!IsPostBack)
        {
            if (boxId.HasValue && receiptId.HasValue)
            {
                FinReceipt finReceipt = new FinReceipt();
                FinBox finBox = new FinBox();
                if (finBox.LoadByPrimaryKey(boxId.Value))
                {
                    txtBoxNo.Text = finBox.BoxNo;
                    txtSupplierBoxNo.Text = finBox.SupplierBoxNo;
                    drpCompany.SelectedValue = finBox.CompanyName + "-" + finBox.CompanyCode;
                    drpFunctionDept.SelectedValue = finBox.FunctionDept;
                    drpStorageStatus.SelectedValue = finBox.StorageStatus;
                }

                if (finReceipt.LoadByPrimaryKey(receiptId.Value))
                {
                    drpEStorageStatus.SelectedValue = finReceipt.EStorageStatus;
                    if (finReceipt.NeedStoreYear.HasValue)
                    { txtNeedStoreYear.Text = finReceipt.NeedStoreYear.Value.ToString(); }
                    //if (finReceipt.FirstStorageDate.HasValue)
                    //{ txtFirstStorageDate.Value = finReceipt.FirstStorageDate.Value.ToString("yyyy-MM-dd"); }

                    txtComments.Text = finReceipt.Comments;
                    txtHisIndexStart.Text = finReceipt.HisIndexStart;
                    txtHisIndexEnd.Text = finReceipt.HisIndexEnd;
                    //drpManageUser.Text = finReceipt.ManageUser;
                    //if (!string.IsNullOrEmpty(finReceipt.HisDocumentStartDate))
                    //{
                    //    switch (finReceipt.HisDocumentStartDate.Length)
                    //    {
                    //        case 4:
                    //            drpHisDocumentYearStart.SelectedValue = finReceipt.HisDocumentStartDate;
                    //            break;
                    //        case 6:
                    //            drpHisDocumentYearStart.SelectedValue = finReceipt.HisDocumentStartDate.Substring(0, 4);
                    //            drpHisDocumentMonthStart.SelectedValue = finReceipt.HisDocumentStartDate.Substring(4, 2);
                    //            break;
                    //        case 8:
                    //            drpHisDocumentYearStart.SelectedValue = finReceipt.HisDocumentStartDate.Substring(0, 4);
                    //            drpHisDocumentMonthStart.SelectedValue = finReceipt.HisDocumentStartDate.Substring(4, 2);
                    //            drpHisDocumentDayStart.SelectedValue = finReceipt.HisDocumentStartDate.Substring(6, 2);
                    //            break;
                    //    }
                    //}

                    //if (!string.IsNullOrEmpty(finReceipt.HisDocumentEndDate))
                    //{
                    //    switch (finReceipt.HisDocumentEndDate.Length)
                    //    {
                    //        case 4:
                    //            drpHisDocumentYearEnd.SelectedValue = finReceipt.HisDocumentEndDate;
                    //            break;
                    //        case 6:
                    //            drpHisDocumentYearEnd.SelectedValue = finReceipt.HisDocumentEndDate.Substring(0, 4);
                    //            drpHisDocumentMonthEnd.SelectedValue = finReceipt.HisDocumentEndDate.Substring(4, 2);
                    //            break;
                    //        case 8:
                    //            drpHisDocumentYearEnd.SelectedValue = finReceipt.HisDocumentEndDate.Substring(0, 4);
                    //            drpHisDocumentMonthEnd.SelectedValue = finReceipt.HisDocumentEndDate.Substring(4, 2);
                    //            drpHisDocumentDayEnd.SelectedValue = finReceipt.HisDocumentEndDate.Substring(6, 2);
                    //            break;
                    //    }
                    //}
                }

            }

        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        FinReceipt finReceipt = new FinReceipt();
        FinBox finBox = new FinBox();
        FinBoxReceipts finBoxReceipts = new FinBoxReceipts();
        string user = LoginUserInfo.CurrentUser.LoginName;
        DateTime today = DateTime.Now;
        string companyCode = drpCompany.SelectedValue;

        //if (!string.IsNullOrEmpty(drpCompany.SelectedValue))
        //{
        //    BFCompanyDic bFCompanyDic = new BFCompanyDic();
        //    bFCompanyDic.LoadByPrimaryKey(decimal.Parse(drpCompany.SelectedValue));
        //    if (bFCompanyDic.es.HasData)
        //    {
        //        companyCode = bFCompanyDic.CompanyCode;
        //    }
        //}



        using (esTransactionScope tr = new esTransactionScope())
        {
            finBox.BoxNo = txtBoxNo.Text.Trim();
            finBox.DocumentType = drpDocumentType.SelectedValue;
            finBox.FunctionDept = drpFunctionDept.SelectedValue;
            if (!string.IsNullOrEmpty(drpThirdUnit.SelectedValue))
            { finBox.BoxLocation = decimal.Parse(drpThirdUnit.SelectedValue); }
            if (!string.IsNullOrEmpty(drpCabin.SelectedValue))
            { finBox.ThirdUnitCabinId = Guid.Parse(drpCabin.SelectedValue); }
            if (!string.IsNullOrEmpty(drpThirdUnit.SelectedValue))
            { finBox.ThirdUnitNumberId = Guid.Parse(drpThirdUnit.SelectedValue); }

            finBox.StorageStatus = drpStorageStatus.SelectedValue;
            finBox.SupplierBoxNo = txtSupplierBoxNo.Text.Trim();
            finBox.UpdatedBy = user;
            finBox.UpdatedDttm = today;
            finBox.VersionNo = finBox.VersionNo+1;
            finBox.CompanyCode = companyCode;
            finBox.Save();

            finReceipt.FunctionDept = drpFunctionDept.SelectedValue;
            finReceipt.StorageStatus = drpStorageStatus.SelectedValue;
            finReceipt.EStorageStatus = drpEStorageStatus.SelectedValue;

            if (!string.IsNullOrEmpty(drpThirdUnit.SelectedValue))
            { finReceipt.StorageLocation = decimal.Parse(drpThirdUnit.SelectedValue); }
            if (!string.IsNullOrEmpty(drpCabin.SelectedValue))
            { finReceipt.ThirdUnitCabinId = Guid.Parse(drpCabin.SelectedValue); }
            if (!string.IsNullOrEmpty(drpThirdUnit.SelectedValue))
            { finReceipt.ThirdUnitNumberId = Guid.Parse(drpThirdUnit.SelectedValue); }


            if (!string.IsNullOrEmpty(txtNeedStoreYear.Text.Trim()))
            { finReceipt.NeedStoreYear = int.Parse(txtNeedStoreYear.Text.Trim()); }
            //if (!string.IsNullOrEmpty(txtDestoryDate.Value))
            //{ finReceipt.DestoryDate = DateTime.Parse(txtDestoryDate.Value); }
            finReceipt.Comments = txtComments.Text.Trim();

            finReceipt.HisIndexStart = txtHisIndexStart.Text.Trim();
            finReceipt.HisIndexEnd = txtHisIndexEnd.Text.Trim();
            finReceipt.HisDocumentStartDate = txtHisDocumentStartDate.Text.Trim();
            finReceipt.HisDocumentEndDate = txtHisDocumentEndDate.Text.Trim();
            finReceipt.UpdatedBy = user;
            finReceipt.UpdatedDttm = today;
            finReceipt.VersionNo = finReceipt.VersionNo+1;
            finReceipt.Save();

            finBoxReceipts.BoxID = finBox.BoxID;
            finBoxReceipts.ReceiptID = finBoxReceipts.ReceiptID;
            finBoxReceipts.CreatedBy = finBoxReceipts.UpdatedBy = user;
            finBoxReceipts.CreatedDttm = finBoxReceipts.UpdatedDttm = today;
            finBoxReceipts.Save();

            tr.Complete();

            WebHelper.CloseDialog(true);
        }
    }


    protected void drpThirdUnit_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(drpThirdUnit.SelectedValue))
        {
            BFThirdUnitCabinCollection bFThirdUnitCabinCollection = new BFThirdUnitCabinCollection();
            bFThirdUnitCabinCollection.Query.Where(bFThirdUnitCabinCollection.Query.ThirdUnitID == decimal.Parse(drpThirdUnit.SelectedValue))
                .OrderBy(bFThirdUnitCabinCollection.Query.Name.Ascending);
            bFThirdUnitCabinCollection.Query.Load();
            drpCabin.DataSource = bFThirdUnitCabinCollection;
            drpCabin.DataTextField = BFThirdUnitCabinMetadata.ColumnNames.Name;
            drpCabin.DataValueField = BFThirdUnitCabinMetadata.ColumnNames.ThirdUnitCabinID;
            drpCabin.DataBind();

            drpCabin.Items.Insert(0, new ListItem("请选择凭证仓库号", ""));
        }
    }

    protected void drpCabin_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(drpCabin.SelectedValue))
        {
            BFThirdUnitNumberCollection bFThirdUnitNumberCollection = new BFThirdUnitNumberCollection();
            bFThirdUnitNumberCollection.Query.Where(bFThirdUnitNumberCollection.Query.ThirdUnitCabinID == Guid.Parse(drpCabin.SelectedValue))
                .OrderBy(bFThirdUnitNumberCollection.Query.Name.Ascending);
            bFThirdUnitNumberCollection.Query.Load();
            drpNumber.DataSource = bFThirdUnitNumberCollection;
            drpNumber.DataTextField = BFThirdUnitNumberMetadata.ColumnNames.Name;
            drpNumber.DataValueField = BFThirdUnitNumberMetadata.ColumnNames.ThirdUnitNumberID;
            drpNumber.DataBind();

            drpNumber.Items.Insert(0, new ListItem("请选择凭证柜号", ""));
        }
    }
}